 
#import "BNDSLNavigationBarViewController.h"
#import "BNDSLNavigationBarConfigure.h"

@interface BNDSLNavigationBarViewController ()

@property (nonatomic, strong) BNDSLNavigationBar   *sl_navigationBar;

@property (nonatomic, strong) UINavigationItem  *sl_navigationItem;

@end

@implementation BNDSLNavigationBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置自定义导航栏
    [self setupCustomNavBar];
    
    // 设置导航栏外观
    [self setupNavBarAppearance];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 隐藏系统导航栏
    [self.navigationController setNavigationBarHidden:YES];
    
    // 将自定义导航栏放置顶层
    if (self.sl_navigationBar && !self.sl_navigationBar.hidden) {
        [self.view bringSubviewToFront:self.sl_navigationBar];
    }
    
    // 获取状态
    self.sl_navigationBar.sl_statusBarHidden = self.sl_statusBarHidden;
    
    // 返回按钮样式
    if (self.sl_backStyle == BNDSLNavigationBarBackStyleNone) {
        self.sl_backStyle = BNDSLConfigure.backStyle;
    }
}

#pragma mark - Public Methods
- (void)showNavLine {
    self.sl_navLineHidden = NO;
}

- (void)hideNavLine {
    self.sl_navLineHidden = YES;
}

- (void)refreshNavBarFrame {
    [self setupNavBarFrame];
}

#pragma mark - private Methods
/**
 设置自定义导航条
 */
- (void)setupCustomNavBar {
    
    [self.view addSubview:self.sl_navigationBar];
    
    [self setupNavBarFrame];
    
    self.sl_navigationBar.items = @[self.sl_navigationItem];
}

/**
 设置导航栏外观
 */
- (void)setupNavBarAppearance {
    BNDSLNavigationBarConfigure *configure = [BNDSLNavigationBarConfigure sharedInstance];
    
    // 设置默认背景色
    if (configure.backgroundColor) {
        self.sl_navBackgroundColor = configure.backgroundColor;
    }
    
    // 设置默认标题颜色
    if (configure.titleColor) {
        self.sl_navTintColor = configure.titleColor;
    }
    
    // 设置默认标题字体
    if (configure.titleFont) { 
        self.sl_navTitleFont = configure.titleFont;
    }
    
    // 设置默认返回图片
    if (self.sl_backImage == nil) {
        self.sl_backImage = configure.backImage;
    }
    
    // 设置默认返回样式
    if (self.sl_backStyle == BNDSLNavigationBarBackStyleNone) {
        self.sl_backStyle = configure.backStyle;
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self setupNavBarFrame];
}

- (void)setupNavBarFrame {
    CGFloat width  = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    CGFloat navBarH = 0;
    if (width > height) { // 横屏
        if (BNDSL_IS_iPad) {
            CGFloat statusBarH = [UIApplication sharedApplication].statusBarFrame.size.height;
            CGFloat navigaBarH = self.navigationController.navigationBar.frame.size.height;
            navBarH = statusBarH + navigaBarH;
        }else if (BNDSL_IS_iPhoneX) {
            navBarH = BNDSL_NAVBAR_HEIGHT;
        }else {
            if (width == 736.0f && height == 414.0f) { // plus横屏
                navBarH = self.sl_statusBarHidden ? BNDSL_NAVBAR_HEIGHT : BNDSL_STATUSBAR_NAVBAR_HEIGHT;
            }else { // 其他机型横屏
                navBarH = self.sl_statusBarHidden ? 32.0f : 52.0f;
            }
        }
    }else { // 竖屏
        navBarH = self.sl_statusBarHidden ? (BNDSL_SAFEAREA_TOP + BNDSL_NAVBAR_HEIGHT) : BNDSL_STATUSBAR_NAVBAR_HEIGHT;
    }
    
    self.sl_navigationBar.frame = CGRectMake(0, 0, width, navBarH);
    self.sl_navigationBar.sl_statusBarHidden = self.sl_statusBarHidden;
    [self.sl_navigationBar layoutSubviews];
}

#pragma mark - 控制状态栏的方法
- (BOOL)prefersStatusBarHidden {
    return self.sl_statusBarHidden;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.sl_statusBarStyle;
}

#pragma mark - 懒加载
- (BNDSLNavigationBar *)sl_navigationBar {
    if (!_sl_navigationBar) {
        _sl_navigationBar = [[BNDSLNavigationBar alloc] initWithFrame:CGRectZero];
    }
    return _sl_navigationBar;
}

- (UINavigationItem *)sl_navigationItem {
    if (!_sl_navigationItem) {
        _sl_navigationItem = [UINavigationItem new];
    }
    return _sl_navigationItem;
}

#pragma mark - setter
- (void)setSl_navTitle:(NSString *)sl_navTitle{
    _sl_navTitle = sl_navTitle;
    
    self.sl_navigationItem.title = sl_navTitle;
}
- (void)setSl_navBarTintColor:(UIColor *)sl_navBarTintColor{
    _sl_navBarTintColor = sl_navBarTintColor;
    
    self.sl_navigationBar.barTintColor = sl_navBarTintColor;
}
 
- (void)setSl_navBackgroundColor:(UIColor *)sl_navBackgroundColor{
    _sl_navBackgroundColor = sl_navBackgroundColor;
    
    [self.sl_navigationBar setBackgroundImage:[UIImage sl_imageWithColor:sl_navBackgroundColor] forBarMetrics:UIBarMetricsDefault];
}
 
- (void)setSl_navBackgroundImage:(UIImage *)sl_navBackgroundImage{
    _sl_navBackgroundImage = sl_navBackgroundImage;
    
    [self.sl_navigationBar setBackgroundImage:sl_navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
 
- (void)setSl_navShadowColor:(UIColor *)sl_navShadowColor{
    _sl_navShadowColor = sl_navShadowColor;
    
    self.sl_navigationBar.shadowImage = [UIImage sl_changeImage:[UIImage sl_imageNamed:@"nav_line"] color:sl_navShadowColor];
}
 
- (void)setSl_navShadowImage:(UIImage *)sl_navShadowImage{
    _sl_navShadowImage = sl_navShadowImage;
    
    self.sl_navigationBar.shadowImage = sl_navShadowImage;
}
 
- (void)setSl_navTintColor:(UIColor *)sl_navTintColor{
    _sl_navTintColor = sl_navTintColor;
    
    self.sl_navigationBar.tintColor = sl_navTintColor;
}

 
- (void)setSl_navTitleView:(UIView *)sl_navTitleView{
    _sl_navTitleView = sl_navTitleView;
    
    self.sl_navigationItem.titleView = sl_navTitleView;
}
 
- (void)setSl_navTitleColor:(UIColor *)sl_navTitleColor{
    _sl_navTitleColor = sl_navTitleColor;
    
    UIFont *titleFont = self.sl_navTitleFont ? self.sl_navTitleFont : BNDSLConfigure.titleFont;
    
    self.sl_navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: sl_navTitleColor, NSFontAttributeName: titleFont};
}
 
- (void)setSl_navTitleFont:(UIFont *)sl_navTitleFont{
    _sl_navTitleFont = sl_navTitleFont;
    
    UIColor *titleColor = self.sl_navTitleColor ? self.sl_navTitleColor : BNDSLConfigure.titleColor;
    
    self.sl_navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: titleColor, NSFontAttributeName: sl_navTitleFont};
}
 
- (void)setSl_navLeftBarButtonItem:(UIBarButtonItem *)sl_navLeftBarButtonItem{
    _sl_navLeftBarButtonItem = sl_navLeftBarButtonItem;
    
    self.sl_navigationItem.leftBarButtonItem = sl_navLeftBarButtonItem;
}
 
- (void)setSl_navLeftBarButtonItems:(NSArray<UIBarButtonItem *> *)sl_navLeftBarButtonItems{
    _sl_navLeftBarButtonItems = sl_navLeftBarButtonItems;
    
    self.sl_navigationItem.leftBarButtonItems = sl_navLeftBarButtonItems;
}
 
- (void)setSl_navRightBarButtonItem:(UIBarButtonItem *)sl_navRightBarButtonItem{
    _sl_navRightBarButtonItem = sl_navRightBarButtonItem;
    
    self.sl_navigationItem.rightBarButtonItem = sl_navRightBarButtonItem;
}
 
- (void)setSl_navRightBarButtonItems:(NSArray<UIBarButtonItem *> *)sl_navRightBarButtonItems{
    _sl_navRightBarButtonItems = sl_navRightBarButtonItems;
    
    self.sl_navigationItem.rightBarButtonItems = sl_navRightBarButtonItems;
}
 
- (void)setSl_navLineHidden:(BOOL)sl_navLineHidden{
    _sl_navLineHidden = sl_navLineHidden;
    
    self.sl_navigationBar.sl_navLineHidden = sl_navLineHidden;
    
    if (@available(iOS 11.0, *)) {
        UIImage *shadowImage = nil;
        if (sl_navLineHidden) {
            shadowImage = [UIImage new];
        }else if (self.sl_navShadowImage) {
            shadowImage = self.sl_navShadowImage;
        }else if (self.sl_navShadowColor) {
            shadowImage = [UIImage sl_changeImage:[UIImage sl_imageNamed:@"nav_line"] color:self.sl_navShadowColor];
        }
        self.sl_navigationBar.shadowImage = shadowImage;
    }
    
    [self.sl_navigationBar layoutSubviews];
}
@end
